<?php

namespace app\api\controller;

use app\admin\model\others\Inform;
use app\common\controller\Api;
use app\common\model\Commission;
use app\common\model\Config;
use app\common\model\DiamondLog;
use app\common\model\game\Modes;
use app\common\model\game\Record;
use app\common\model\others\Banner;
use app\common\model\pay\Order;
use think\Cache;
use think\Db;
use think\Exception;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $third_url = Db::name('config')->where(['name' => 'third_url'])->value("value", "");
        $third_url_array = explode(",", $third_url);
        $index = Cache::get('url_index');
        $this->success('请求成功', $third_url_array[$index]);
    }

    public function version()
    {
        $this->success("", Db::name('version')->order('id DESC')->find());
    }

    public function club_info()
    {
        $efficacious_num = Config::where('name', "efficacious_num")->value('value', "");
        $month_earnings = Config::where('name', "month_earnings")->value('value', "");
        $this->success("", ["month_earnings" => $month_earnings, "efficacious_num" => $efficacious_num]);
    }

    public function banner()
    {
        $this->success("", Banner::where('status', 1)->field("url,image")->select());
    }

    public function agreement()
    {
        $name = $this->request->param('name', "");
        if (empty($name)) {
            $this->error("参数有误");
        }
        $this->success("", Config::where(['name' => $name, 'group' => 'protocol'])->value('value'));
    }

    public function get_config()
    {
        $name = $this->request->param('name');
        $this->success("", Config::where('name', $name)->where("group", "neq", "alipay")->value('value', ""));
    }

    public function get_group()
    {
        $group = $this->request->param("group");
        $this->success(Config::where('group', $group)->where("group", "neq", "alipay")->where('name', "<>", "forbiddenip")->column("value", "name"));
    }

    public function inform_lists()
    {
        $size = $this->request->param("size", 10);
        $this->success("", Inform::field("id,title")->paginate($size));
    }

    public function inform_content()
    {
        $id = $this->request->param('id', 0);
        $this->success("", Inform::field("id,title,content")->find($id));
    }

    public function area()
    {
        $params = $this->request->get("row/a");
        if (!empty($params)) {
            $province = isset($params['province']) ? $params['province'] : null;
            $city = isset($params['city']) ? $params['city'] : null;
        } else {
            $province = $this->request->get('province', null);
            $city = $this->request->get('city', null);
        }
        $where = ['pid' => 0, 'level' => 1];
        $provincelist = null;
        if (!empty($province)) {
            $where['pid'] = $province;
            $where['level'] = 2;
            if (!empty($city)) {
                $where['pid'] = $city;
                $where['level'] = 3;
            }
        }
        $provincelist = Db::name('area')->where($where)->field('id as value,name')->select();
        $this->success('', $provincelist);
    }

//    public function division()
//    {
//        $record = Record::where('is_allot', 0)->select();
//        $msg = "";
//        if ($record) {
//
//            Db::startTrans();
//            try {
//
//                foreach ($record as $vr) {
//
//                    $order = Order::where(['user_id' => $vr['user_id'], 'status' => 1])->find();
//
//                    if ($order) {
//                        $parent_id = \app\common\model\User::where("id", $vr['user_id'])->value("parent_id", 0);//获取上级
//                        if ($parent_id != 0) {//上级验证
//                            $ratio = Modes::where("id", $vr['mode_id'])->value("divide_ratio");
//                            $ratio = $ratio / 100;//比例校正
//                            $diamond = bcmul($vr['service'], $ratio, 2);//收益数
//                            $user = \app\common\model\User::get($parent_id);
//                            if ($user && $diamond != 0) {
//                                $before = $user['diamond'];
//                                $after = function_exists("bcadd") ? bcadd($user['diamond'], $diamond, 2) : $user['diamond'] + $diamond;
//                                $user->save(['diamond' => $after]);
//                                DiamondLog::create([
//                                    'user_id' => $user['id'], 'diamond' => $diamond,
//                                    'before' => $before, 'after' => $after,
//                                    'memo' => "邀请收益", 'type' => 1
//                                ]);
//
//                                $parent_club = \app\common\model\User::where("id", $user['parent_id'])->value("is_club", 0);
//                                if ($parent_club != 0) {
//
//                                    $club_user = \app\common\model\User::get($user['parent_id']);
//                                    $club_diamond = bcmul($diamond, $ratio, 2);
//                                    if ($club_user && $club_diamond != 0) {
//
//                                        $club_before = $club_user['diamond'];
//                                        $club_after = function_exists("bcadd") ? bcadd($club_diamond, $club_user['diamond'], 2) : $club_user['diamond'] + $club_diamond;
//                                        $club_user->save(['diamond' => $club_after]);
//                                        DiamondLog::create([
//                                            'user_id' => $club_user['id'], 'diamond' => $club_diamond,
//                                            'before' => $club_before, 'after' => $club_after,
//                                            'memo' => "推广收益", 'type' => 4
//                                        ]);
//                                    }
//                                }
//                            }
//                        }
//                        Record::update(['is_allot' => 1], ['id' => $vr['id']]);
//                    } else {
//                        $msg .= $vr['user_id'] . "未充值-";
//                    }
//
//                }
//                $msg = "完成";
//                Db::commit();
//            } catch (Exception $exception) {
//                Db::rollback();
//                $msg .= $exception->getMessage() . "-";
//            }
//
//        } else {
//            $msg = "无分佣";
//        }
//
//        $this->success($msg);
//
//    }
//
//    public function divisions()
//    {
//        $record = Record::where('is_allot', 0)->select();
//        $msg = "";
//        if ($record) {
//            Db::startTrans();
//            try {
//                $expenses_ratio = Config::where('name', 'expenses_ratio')->value('value', 15);
//                foreach ($record as $vr) {
//                    $parent_id_one = \app\common\model\User::where("id", $vr['user_id'])->value("parent_id", 0);//获取上级
//                    $ratio_residue = 100 - $expenses_ratio;
//                    $ratio_adjust = $ratio_residue / 100;
//                    $service = bcmul($vr['service'], $ratio_adjust, 4);
//                    $ratio = Commission::ratio($parent_id_one, $service);//计算佣金
//
//                    if ($ratio['one'] > 0) {
//                        $one = \app\common\model\User::get($parent_id_one);
//                        if ($one) {
//                            $msg .= $vr['id'] . "-1:" . $ratio['one'] . "-service:" . $service . "-";
//                            $before = $one['diamond'];
//                            $after = function_exists("bcadd") ? bcadd($one['diamond'], $ratio['one'], 2) : $one['diamond'] + $ratio['one'];
//                            $one->save(['diamond' => $after]);
//                            DiamondLog::create([
//                                'user_id' => $one['id'], 'diamond' => $ratio['one'],
//                                'before' => $before, 'after' => $after,
//                                'memo' => "邀请收益", 'type' => 1
//                            ]);
//                            $two = \app\common\model\User::get($one['parent_id']);
//                            if ($ratio['two'] > 0 && $two) {
//                                $msg .= $vr['id'] . "-2:" . $ratio['two'] . "--";
//                                $club_before = $two['diamond'];
//                                $club_after = function_exists("bcadd") ? bcadd($ratio['two'], $two['diamond'], 2) : $two['diamond'] + $ratio['two'];
//                                $two->save(['diamond' => $club_after]);
//                                DiamondLog::create([
//                                    'user_id' => $two['id'], 'diamond' => $ratio['two'],
//                                    'before' => $club_before, 'after' => $club_after,
//                                    'memo' => "推广收益", 'type' => 4
//                                ]);
//                            }
//                        }
//                    }
//                    Record::update(['is_allot' => 1, 'one' => $ratio['one'], 'two' => $ratio['two']], ['id' => $vr['id']]);
//                }
//                $msg .= "完成";
//                Db::commit();
//            } catch (Exception $e) {
//                Db::rollback();
//                $msg = $e->getMessage();
//            }
//        }
//        $this->success($msg);
//    }

//    public function articlelists()
//    {
//        $tag = $this->request->param('tag');
//        if (!$tag) {
//            $this->error("当前请求有误");
//        }
//        $type_id = Type::where('tag', $tag)->value("id", 0);
//
//        $this->success("", Lists::where("type_id", $type_id)->select());
//    }
//
//    public function articlecontent()
//    {
//        $id = $this->request->param("id");
//        if (!$id) {
//            $this->error("当前内容有误");
//        }
//
//        $article = Lists::get($id);
//        $article['content'] = Content::where('article_id', $id)->value("contents");
//        $this->success("", $article);
//    }
}
